package com.service.auth.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.service.auth.client.UcenterMemberClient;
import com.service.utils.CookieUtils;
import com.service.utils.JwtUtils;
import com.service.utils.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@Api(description = "用户登录token授权")
public class AuthController {

    @Autowired
    private UcenterMemberClient ucenterMemberClient;

    @GetMapping("wxCreateToken")
    public ResultVo wxCreateToken(@RequestParam("id") String id){
        if (StringUtils.isEmpty(id))return  ResultVo.error();
        String token = JwtUtils.getJwtToken(id);
        //写入cookie中

        return ResultVo.ok().data("token",token);
    }

    @PostMapping("login")
    @ApiOperation("用户登录,登录成功，将用户信息生成token存入cookie中")
    public ResultVo login(
            @ApiParam(name = "mobile", value = "手机号", required = true) @RequestParam("mobile") String mobile,
            @ApiParam(name = "password", value = "密码", required = true) @RequestParam("password") String password,
            HttpServletRequest request,
            HttpServletResponse response) {
        ResultVo resultVo = ucenterMemberClient.login(mobile, password);
        //如果登录成功就把生成的token放入cookie中
        if (resultVo.getCode() == 20000) {
            Map<String, Object> map = JSONObject.parseObject(JSON.toJSON(resultVo.getData().get("item")).toString(), HashMap.class);
            String token = JwtUtils.getJwtToken(map.get("id").toString());
            //写入cookie中
            CookieUtils.setCookie(request, response, "GULI-LOGIN", token, 1800, "utf-8", true);
            return ResultVo.ok();
        }
        return ResultVo.error().message(resultVo.getMessage());
    }

    /**
     * 解析token信息，返回给前台页面
     * 并刷新token信息
     *
     * @param token
     * @return
     */
    @GetMapping("verify")
    public ResultVo verify(
           @RequestParam(required = false)  String token,
            HttpServletResponse response,
            HttpServletRequest request
    ) {
        if (StringUtils.isEmpty(token)){
             token = CookieUtils.getCookieValue(request, "GULI-LOGIN");
            if (token==null){
                return ResultVo.error();
            }
        }

        String id = "";
        try {
            //解析token信息  获取用户登录id
            id = JwtUtils.getMemberIdByJwtToken(token);
            //判断用户登录id是否为空
            if (id == null) {
                return ResultVo.error();
            }
            //重新加密用户信息 创建新的token信息
            token = JwtUtils.getJwtToken(id);
            //写入cookie中
            CookieUtils.setCookie(request, response, "GULI-LOGIN", token, 1800, "utf-8", true);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ucenterMemberClient.queryById(id);
    }

    /**
     * 解析token信息，返回给前台页面
     * 并刷新token信息
     *
     * @return
     */
    @GetMapping("logout")
    public ResultVo logout(
            HttpServletResponse response,
            HttpServletRequest request
    ) {
        try {
            //设置值为空  让cookie信息立刻失效
            CookieUtils.setCookie(request, response, "GULI-LOGIN", "", 1800, "utf-8", true);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

}
